<template>
  <div>
    <div class="search-term">
      <el-form :inline="true" :model="searchInfo" class="demo-form-inline">
        <el-form-item label="按钮名（中）">
          <el-input v-model="searchInfo.cname" placeholder="搜索条件" />
        </el-form-item>
        <el-form-item label="按钮名（英）">
          <el-input v-model="searchInfo.name" placeholder="搜索条件" />
        </el-form-item>

        <el-form-item label="按钮组">
          <el-input v-model="searchInfo.buttonGroup" placeholder="搜索条件" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="openDialog">新增按钮</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      ref="multipleTable"
      :data="tableData"
      border
      stripe
      style="width: 100%"
      tooltip-effect="dark"
    >
      <el-table-column type="selection" width="55" />

      <el-table-column label="按钮名（中）" prop="cname" />

      <el-table-column label="按钮名（英）" prop="name" />

      <el-table-column label="所属菜单" prop="menu.title" />

      <el-table-column label="按钮组" prop="buttonGroup" />

      <el-table-column label="操作" width="129">
        <template slot-scope="scope">
          <el-button size="small" type="primary" @click="updateSysButton(scope.row)">变更</el-button>
          <el-popconfirm
            title="确认删除？"
            confirm-button-text="确定"
            cancel-button-text="取消"
            icon="el-icon-info"
            icon-color="red"
            placement="top-end"
            @confirm="deleteSysButton(scope.row)"
            @cancel="scope.row.visible = false"
          >
            <el-button slot="reference" type="danger" size="mini" style="margin-left: 10px">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      :current-page="page"
      :page-size="pageSize"
      :page-sizes="[10, 30, 50, 100]"
      :style="{float:'right',padding:'20px'}"
      :total="total"
      layout="total, sizes, prev, pager, next, jumper"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />

    <el-dialog :before-close="closeDialog" :visible.sync="dialogFormVisible" title="弹窗操作" width="40%">
      <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-position="top">
        <el-form-item label="所属菜单" prop="menuId">
          <el-cascader
            v-model="formData.menuId"
            :options="menuOptions"
            :props="{ expandTrigger: 'hover', label: 'title', value: 'ID' }"
            filterable
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="按钮名（中）" prop="cname">
          <el-input v-model="formData.cname" placeholder="请输入按钮名（中）" clearable :style="{width: '100%'}" />
        </el-form-item>
        <el-form-item label="按钮名（英）" prop="name">
          <el-input v-model="formData.name" placeholder="请输入按钮名（英）" clearable :style="{width: '100%'}" />
        </el-form-item>
        <el-form-item label="按钮组" prop="buttonGroup">
          <el-input v-model="formData.buttonGroup" placeholder="请输入按钮组" clearable :style="{width: '100%'}" />
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="enterDialog">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { getMenuList } from '@/api/menu'
import {
  getSysButtonList,
  findSysButton,
  createSysButton,
  updateSysButton,
  deleteSysButton
} from '@/api/sysButton'
import infoList from '@/mixins/infoList'
export default {
  name: 'SysButton',
  filters: {

  },
  mixins: [infoList],
  data() {
    return {
      listApi: getSysButtonList,
      dialogFormVisible: false,
      visible: false,
      type: '',
      formData: {
        name: null, cname: null, buttonGroup: null, menuId: null
      },
      menuOptions: [],
      rules: {
        menuId: [
          {
            required: true,
            message: '请选择所属菜单',
            trigger: 'blur'
          }
        ],
        cname: [{
          required: true,
          message: '请输入按钮名（中）',
          trigger: 'blur'
        }],
        name: [{
          required: true,
          message: '请输入按钮名（英）',
          trigger: 'blur'
        }],
        buttonGroup: [{
          required: true,
          message: '请输入按钮组',
          trigger: 'blur'
        }]
      }
    }
  },
  async created() {
    this.getTableData()
    this.getMenuList()
  },
  methods: {
    // 条件搜索前端看此方法
    onSubmit() {
      this.page = 1
      this.pageSize = 10
      if (this.searchInfo.status === '') {
        this.searchInfo.status = null
      }
      this.getTableData()
    },
    // 获取菜单
    getMenuList() {
      const params = {
        page: 1,
        pageSize: 999
      }
      getMenuList(params).then((res) => {
        if (res.code === 0) {
          this.menuOptions = res.data.list
        }
      })
    },
    async updateSysButton(row) {
      const res = await findSysButton({ ID: row.ID })
      this.type = 'update'
      if (res.code === 0) {
        this.formData = res.data.resysButton
        this.dialogFormVisible = true
      }
    },
    closeDialog() {
      this.dialogFormVisible = false
      this.formData = {
        name: null,
        cname: null,
        buttonGroup: null
      }
    },
    async deleteSysButton(row) {
      this.visible = false
      const res = await deleteSysButton({ ID: row.ID })
      if (res.code === 0) {
        this.$notify({
          type: 'success',
          title: '成功',
          message: '删除成功'
        })
        this.getTableData()
      }
    },
    async enterDialog() {
      this.$refs['elForm'].validate(async valid => {
        if (!valid) return
        const params = JSON.parse(JSON.stringify(this.formData))
        if (Array.isArray(params.menuId)) {
          params.menuId = params.menuId[params.menuId.length - 1]
        }
        let res
        switch (this.type) {
          case 'update':
            res = await updateSysButton(params)
            break
          default:
            res = await createSysButton(params)
        }
        if (res.code === 0) {
          this.$notify({
            type: 'success',
            title: '成功',
            message: '操作成功'
          })
          this.closeDialog()
          this.getTableData()
        }
      })
    },
    openDialog() {
      this.type = 'create'
      this.dialogFormVisible = true
    }
  }
}
</script>

<style>
</style>
